﻿#include "FTheme.h"

QColor FColors::getBaseColor(FColors::FColorType type)
{
    switch (type) {
    case Primary: return QColor(0x40, 0x9e, 0xff, 0xBB); // 蓝
    case Success: return QColor(0x67, 0xc2, 0x3a, 0xBB); // 绿
    case Info:    return QColor(0x90, 0x93, 0x99, 0xBB); // 灰
    case Warning: return QColor(0xe6, 0xa2, 0x3c, 0xBB); // 黄
    case Danger:  return QColor(0xf5, 0x6c, 0x6c, 0xBB); // 红
    default:
    case Default: return QColor(0xdc, 0xdf, 0xe6, 0xBB); // 默认（灰）
    }
}

QColor FColors::getHoverColor(FColors::FColorType type)
{
    if (type == Default) return QColor(0xc0, 0xc4, 0xcc, 0xBB);
    else return getBaseColor(type).lighter(120);
}

QColor FColors::getActiveColor(FColors::FColorType type)
{
    if (type == Default) return QColor(0x40, 0x9e, 0xff, 0xBB);
    else return getBaseColor(type).darker(170);
}

QColor FColors::getDisabledColor(FColors::FColorType type)
{
   if (type == Default) return QColor(0xf5, 0xf7, 0xfa, 0xBB);
   else return getBaseColor(type).lighter(120);
}

QList<QColor> FColors::getLinearColors(FColors::FColorType type, int count)
{
    QList<QColor> colors;
    if (count <= 0) return colors;
    QColor base = getBaseColor(type);
    if (count == 1) { colors.append(base); return colors; }

    // 使用 lighter/darker 生成两个端点色，然后在端点之间线性插值
    const int lightFactor = 120; // 较亮端（120 -> lighter 20%）
    const int darkFactor  = 110; // 较暗端（110 -> darker 10%）
    QColor light = base.lighter(lightFactor);
    QColor dark  = base.darker(darkFactor);

    for (int i = 0; i < count; ++i) {
        double t = static_cast<double>(i) / static_cast<double>(count - 1); // 0..1
        int r = static_cast<int>(light.red()   + t * (dark.red()   - light.red()));
        int g = static_cast<int>(light.green() + t * (dark.green() - light.green()));
        int b = static_cast<int>(light.blue()  + t * (dark.blue()  - light.blue()));
        int a = static_cast<int>(light.alpha() + t * (dark.alpha() - light.alpha()));
        colors.append(QColor(qBound(0, r, 255), qBound(0, g, 255), qBound(0, b, 255), qBound(0, a, 255)));
    }
    return colors;
}

QColor FColors::getBackgroundColor()
{
    return QColor(0xfc, 0xfc, 0xfc, 0x77);
}

QColor FColors::getLineColor()
{
    return getHoverColor(FColors::Default);
}

QColor FColors::getTextColor(bool isEnabled)
{
    return isEnabled ? QColor(0x00, 0x00, 0x00) : QColor(0xb4, 0xb6, 0xc9);
}

FTheme::FSizeType FTheme::getDefaultSize()
{
    return FSizeType::Small;
}

QString FTheme::fstylesheet()
{
    return QString(R"(
QWidget {
    font-family: "Microsoft Yahei";
    font-size: %1px;
    color: %3;
}

QWidget:disabled {
    color: %4;
}

QLabel {
    background: transparent;
}

QGroupBox {
    font-size: %2px;
}

QScrollArea {
    border: none;
    background: transparent;
}

QScrollArea > QWidget > QWidget {
    background: transparent;
}

QCheckBox::indicator {
    width: %1px;
    height: %1px;
}

QDockWidget::title {
    background: %5;
    padding: 6px;
}

/* 分割线 */
QWidget::separator,
QSplitter::handle {
    background: %5;
    border: none;
    padding: 0px;
}

QSplitter::handle:horizontal {
    width: 2px;
    margin: 0 1px;
}

QSplitter::handle:vertical {
    height: 2px;
    margin: 1px 0;
}
QWidget::separator:hover,
QSplitter::handle:hover {
    background-color: %4;
}

/* 垂直滚动条 */
QScrollBar:vertical {
    border: none;
    background: #77F0F2F4;
    width: 10px;
    margin: 0px;
}

QScrollBar::handle:vertical {
    background: #7796A3B0;
    border-radius: 5px;
    min-height: 25px;
}

QScrollBar::handle:vertical:hover {
    background: #774F6072;
}

QScrollBar::handle:vertical:pressed {
    background: #77000000;
}

QScrollBar::add-line:vertical,
QScrollBar::sub-line:vertical {
    border: none;
    background: none;
    height: 0px;
}

/* 水平滚动条 */
QScrollBar:horizontal {
    border: none;
    background: #77F0F2F4;
    height: 10px;
    margin: 0px;
}

QScrollBar::handle:horizontal {
    background: #7796A3B0;
    border-radius: 5px;
    min-width: 25px;
}

QScrollBar::handle:horizontal:hover {
    background: #774F6072;
}

QScrollBar::handle:horizontal:pressed {
    background: #77000000;
}

QScrollBar::add-line:horizontal,
QScrollBar::sub-line:horizontal {
    border: none;
    background: none;
    width: 0px;
}
)").arg(QString::number(FTheme::getDefaultSize()), QString::number(FTheme::getDefaultSize() + 3),
    FColors::getTextColor(true).name(QColor::HexArgb), FColors::getTextColor(false).name(QColor::HexArgb),
    FColors::getBaseColor(FColors::Default).name(QColor::HexArgb));
}
